<?php

class User {
	
	public $id_user;
	public $id_client;
	public $username;
	public $password;
    public $nonCryptedPassword;
	public $nume;
	public $prenume;
	public $change_pass;
	public $tip;  				//0 - Dispecer; 1 - Contabil; 2 - Manager
	public $email;
	public $language;
	
	public $deleted;

	public $tooltip;
    public $extra_text = 1;
    public $version = 0;         //Numarul de modificari
    public $firme_per_pag = 10;
    public $comenzi_per_pag = 10;
    public $coloane_firme;
	public $coloane_comenzi;
	
	public $comenzi_plata_per_pag = 10;
	public $comenzi_incasat_per_pag = 10;
	public $comenzi_incarcari_per_pag = 10;
	public $comenzi_descarcari_per_pag = 10;
  
	public $moneda = 0; 			// moneda implicita pentru fiecare user
	
	public $comanda_interna_externa = 0; 	// tipul de comanda implicita per user: externa/interna
  
	public function __construct($id_client, $username, $password, $tip, $email) {
		
		$this->id_user = CT_NOT_SAVED_YET;
		$this->id_client = (int) $id_client;
		$this->username = $username;
        $this->password = $password;
		$this->nonCryptedPassword = $password;
        
		$this->tip = (int) $tip;
		$this->email = $email;

		$this->change_pass = CT_MUST_CHANGE_PASSWORD;
		$this->language = 0; //default language?

		$this->tooltip = (int) 1; //default tooltip, adica sa afiseze
        $this->comanda_interna_externa = CT_COMANDA_EXTERNA;
		$this->version = 0;
	}
	
	public function queryInsert($db) {
		
		$query = $db->prepare("INSERT INTO `user`  ( 	`username` ,			`password` , 
														`nume` ,				`prenume`,
														`tip` ,					`id_client`,
	                                					`email` ,        	    
		                                				`change_pass` , 	    `language`, 
		                                				`version`,
		                                				`tooltip`, `firme_per_pag`, `comenzi_per_pag`,
		                                				`coloane_firme`, `coloane_comenzi`,
		                                				`moneda`, `comenzi_plata_per_pag`,
		                                				`comenzi_incasat_per_pag`,
		                                				`comanda_interna_externa`,
		                                				`comenzi_incarcari_per_pag`,
		                                				`comenzi_descarcari_per_pag`,
                                                        `extra_text`) 		
                                                                                        		            		
                    					VALUES (		:username, 		      :password, 
		            									:nume, 			      :prenume,
		            									:tip, 			      :id_client,
	                      								:email, 			  
	                      								:change_pass,		  :language,				
	                      								:version,
	                      								:tooltip, 10, 10, 
	                      								'nume,tip,tara,oras,tel_fix,tel_fax,tel_mobil,email,web',
	                      								'id,data_comenzii,id_firma_sol,nr_comanda_sol,nr_factura_sol,suma_totala_sol,id_firma_exec,nr_comanda_exec,nr_factura_exec,suma_totala_exec,data_scadenta_sol,data_scadenta_exec,data_incarcarii,nr_inmatriculare'," . 
	                      								CT_MONEDA_IMPLICITA_USER . ",
	                      								:comenzi_plata_per_pag,
	                      								:comenzi_incasat_per_pag,
	                      								:comanda_interna_externa,
	                      								:comenzi_incarcari_per_pag, 
	                      								:comenzi_descarcari_per_pag,
                                                        :extra_text
		                      			)");
		
		$query->bindParam(':username', 		($this->username), PDO::PARAM_STR);
		$this->password = crypt_password($this->username, $this->password);
		
		$query->bindParam(':password', 		($this->password), PDO::PARAM_STR);
		$query->bindParam(':nume', 			($this->nume), PDO::PARAM_STR);
		
		$query->bindParam(':prenume', 		($this->prenume), PDO::PARAM_STR);
		$query->bindParam(':tip', 			($this->tip), PDO::PARAM_INT);
		$query->bindParam(':id_client', 	($this->id_client), PDO::PARAM_INT);
		$query->bindParam(':email', 		($this->email), PDO::PARAM_STR);

		$query->bindParam(':change_pass', 	($this->change_pass), PDO::PARAM_INT);
		$query->bindParam(':language', 		($this->language), PDO::PARAM_INT);

		$query->bindParam(':version', 		($this->version), PDO::PARAM_INT);
		$query->bindParam(':tooltip', 		($this->tooltip), PDO::PARAM_INT);
		
		$query->bindParam(':comenzi_plata_per_pag', 		($this->comenzi_plata_per_pag), PDO::PARAM_INT);
		$query->bindParam(':comenzi_incasat_per_pag', 		($this->comenzi_incasat_per_pag), PDO::PARAM_INT);

		$query->bindParam(':comanda_interna_externa', 		($this->comanda_interna_externa), PDO::PARAM_INT);

		$query->bindParam(':comenzi_incarcari_per_pag', 		($this->comenzi_incarcari_per_pag), PDO::PARAM_INT);
        $query->bindParam(':comenzi_descarcari_per_pag',         ($this->comenzi_descarcari_per_pag), PDO::PARAM_INT);                
		$query->bindParam(':extra_text', 		($this->extra_text), PDO::PARAM_INT);				
		$username = $this->username;
        $email = $this->email;
                
		try {
			
            $q = "SELECT username, email FROM user
                    WHERE deleted = 0 AND
                        (username = '" . $username . "' OR 
                         email = '" . $email . "');";
                     
			$result = $db->query($q);
            
            if ($result === FALSE) {            
                throw new QueryInsertUserException();
            }
            else {
                if ($row = $result->fetch(PDO::FETCH_ASSOC)) {
                    if ($row['username'] == $username)
                        throw new QueryDuplicateUsernameUserException("Username-ul trebuie sa fie unic!");
                    else
                        throw new QueryDuplicateEmailUserException("Email-ul trebuie sa fie unic!");
                }
            }
			
			if ($query->execute() === FALSE) {
				$errInfo = $query->errorInfo();
				$errInfo = $errInfo[2];
				$query->closeCursor();			
				throw new QueryInsertUserException("Error inserting user " . $this->username . $errInfo);
			}
							
			$this->id_user = (int) $db->lastInsertId();		
			$query->closeCursor();
			return $this->id_user; //return the user id
		}
		catch (QueryInsertUserException $e) {			
			$query->closeCursor();
			throw new QueryInsertUserException("Error inserting user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryDuplicateUsernameUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateUsernameUserException("Error inserting user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryDuplicateEmailUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateEmailUserException("Error inserting user: " . $this->username . $e->getMessage() . $errInfo);
		}	
	}
	
	public function queryUpdate($db) {
	    $this->username = strtolower($this->username);
	    $this->version++;
	    
	    $query = $db->prepare("UPDATE user
	                			SET `username` = :username,			 
									`nume` = :nume,				    `prenume` = :prenume,
									`tip` = :tip,				    `id_client` = :id_client,
                        			`email` = :email,        	    
                        			`language` = :language, 
                        			`version` = :version,
                        			`tooltip` = :tooltip,
                                    `extra_text` = :extra_text,
                                    `comenzi_per_pag` = :comenzi_per_pag,
                                    `firme_per_pag` = :firme_per_pag,
                                    `coloane_firme` = :coloane_firme,
                                    `coloane_comenzi` = :coloane_comenzi,
                                    `moneda` = :moneda_implicita,
                                    `comenzi_plata_per_pag` = :comenzi_plata_per_pag,
                                    `comenzi_incasat_per_pag` = :comenzi_incasat_per_pag,
                                    `comanda_interna_externa` = :comanda_interna_externa,
                                    `comenzi_incarcari_per_pag` = :comenzi_incarcari_per_pag,
                                    `comenzi_descarcari_per_pag` = :comenzi_descarcari_per_pag                                   
			                	WHERE id = :id_user
	                			AND version < :version");

	    $query->bindParam(':moneda_implicita', 		($this->moneda), PDO::PARAM_INT);
	    $query->bindParam(':username', 		($this->username), PDO::PARAM_STR);
	    $query->bindParam(':nume', 			($this->nume), PDO::PARAM_STR);
	    $query->bindParam(':prenume', 		($this->prenume), PDO::PARAM_STR);
	    $query->bindParam(':tip', 			($this->tip), PDO::PARAM_INT);
	    $query->bindParam(':id_client', 	($this->id_client), PDO::PARAM_INT);
	    $query->bindParam(':email', 		($this->email), PDO::PARAM_STR);

	    $query->bindParam(':language', 		($this->language), PDO::PARAM_INT);

	    $query->bindParam(':version', 		($this->version), PDO::PARAM_INT);
	    $query->bindParam(':id_user', 		($this->id_user), PDO::PARAM_INT);
        $query->bindParam(':tooltip',       ($this->tooltip), PDO::PARAM_INT);
        $query->bindParam(':extra_text',    ($this->extra_text), PDO::PARAM_INT);
        $query->bindParam(':comenzi_per_pag',         ($this->comenzi_per_pag), PDO::PARAM_INT);
        $query->bindParam(':firme_per_pag',           ($this->firme_per_pag), PDO::PARAM_INT);
        
        $query->bindParam(':coloane_comenzi',         ($this->coloane_comenzi), PDO::PARAM_STR);
	    $query->bindParam(':coloane_firme', 		  ($this->coloane_firme), PDO::PARAM_STR);
		
	    $query->bindParam(':comenzi_plata_per_pag', 		($this->comenzi_plata_per_pag), PDO::PARAM_INT);
		$query->bindParam(':comenzi_incasat_per_pag', 		($this->comenzi_incasat_per_pag), PDO::PARAM_INT);	    
		
		$query->bindParam(':comanda_interna_externa', 		($this->comanda_interna_externa), PDO::PARAM_INT);		

		$query->bindParam(':comenzi_incarcari_per_pag', 		($this->comenzi_incarcari_per_pag), PDO::PARAM_INT);
		$query->bindParam(':comenzi_descarcari_per_pag', 		($this->comenzi_descarcari_per_pag), PDO::PARAM_INT);	
				
		$username = $this->username;
        $email = $this->email;
        $id = $this->id_user;
        
        $q = "SELECT * FROM user
                WHERE 
                    (username = '$username'
                    OR email = '$email')
                    AND id <> $id";

		try {
			
			$result = $db->query($q);                                
            if ($result === FALSE) {
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];

                throw new QueryUpdateUserException($errInfo);
            }    
            else {                                                
                $count = 0;
                while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
					$row2 = $row;
                    $count++;
                }
            }
            if ($count > 0)
				if ($row2['username'] == $username)
					throw new QueryDuplicateUsernameUserException("Username-ul trebuie sa fie unic!");
				else
					throw new QueryDuplicateEmailUserException("Email-ul trebuie sa fie unic!");			
                       
			if ($query->execute() === FALSE) {
				$errInfo = $query->errorInfo();
				$errInfo = $errInfo[2];
				$query->closeCursor();

				throw new QueryUpdateUserException("Failed updating user's " . $this->username . " profile" . $errInfo);   
			}
			if ($query->rowCount() == 0) { 
				$query->closeCursor();
				throw new QueryWrongVersionException("Query update wrong version on user: " . $this->username);
			}
					
			$query->closeCursor();

			return CT_RESULT_OK;
		}		
		catch (QueryUpdateUserException $e) {			
			$query->closeCursor();
			throw new QueryUpdateUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryWrongVersionException $e) {			
			$query->closeCursor();
			throw new QueryWrongVersionException("Query update wrong version on use: " . $this->username . $e->getMessage() . $errInfo);
		}		
		catch (QueryDuplicateUsernameUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateUsernameUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryDuplicateEmailUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateEmailUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
  	}
  	
  	public function queryLightUpdate($db) {
  		
	    $this->username = strtolower($this->username);
	    $this->version++;
	    $query = $db->prepare("UPDATE user
	                			SET `username` = :username,			
									`nume` = :nume,				    
									`prenume` = :prenume,
									`tip` = :tip,				    
                        			`email` = :email,
                                    `version` = :version
			                	WHERE id = :id_user
	                			AND version < :version");
	    
	    $query->bindParam(':username', 		($this->username), PDO::PARAM_STR);
	    $query->bindParam(':nume', 			($this->nume), PDO::PARAM_STR);
	    $query->bindParam(':prenume', 		($this->prenume), PDO::PARAM_STR);
	    $query->bindParam(':tip', 			($this->tip), PDO::PARAM_INT);
	    $query->bindParam(':email', 		($this->email), PDO::PARAM_STR);
	    $query->bindParam(':version', 		($this->version), PDO::PARAM_INT);
	    $query->bindParam(':id_user', 		($this->id_user), PDO::PARAM_INT);

		$username = $this->username;
        $email = $this->email;
        $id = $this->id_user;
        $q = "SELECT * FROM user
                WHERE 
                    (username = '$username'
                    OR email = '$email')
                    AND id <> $id";

		try {
			$db->beginTransaction();
			
			$result = $db->query($q);                                
            if ($result === FALSE) {
                $errInfo = $query->errorInfo();
                $errInfo = $errInfo[2];
                throw new QueryUpdateUserException($errInfo);
            }    
            else {                                                
                $count = 0;
                while($row = $result->fetch(PDO::FETCH_ASSOC)) {
					$row2 = $row;
                    $count++;
                }
            }
            if ($count > 0)
				if ($row2['username'] == $username)
					throw new QueryDuplicateUsernameUserException("Username-ul trebuie sa fie unic!");
				else
					throw new QueryDuplicateEmailUserException("Email-ul trebuie sa fie unic!");			                       

			if ($query->execute() === FALSE) {
				$errInfo = $query->errorInfo();
				$errInfo = $errInfo[2];
				$query->closeCursor();                     
				$this->version--;
				throw new QueryUpdateUserException("Failed updating light user's " . $this->username . " profile" . $errInfo);   
			}
			if ($query->rowCount() == 0) {
				$errInfo = $query->errorInfo();
				$errInfo = $errInfo[2];
				$this->version--;
				$query->closeCursor();
				throw new QueryWrongVersionException("Query update light wrong version on user: " . $this->username . $errInfo);
			}
					
			$query->closeCursor();
			$db->commit();
			return CT_RESULT_OK;
		}
		catch (QueryUpdateUserException $e) {			
			$query->closeCursor();
			throw new QueryUpdateUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryWrongVersionException $e) {			
			$query->closeCursor();
			throw new QueryWrongVersionException("Query update wrong version on use: " . $this->username . $e->getMessage() . $errInfo);
		}	
		catch (QueryDuplicateUsernameUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateUsernameUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
		catch (QueryDuplicateEmailUserException $e) {			
			$query->closeCursor();
			throw new QueryDuplicateEmailUserException("Error updating user: " . $this->username . $e->getMessage() . $errInfo);
		}
		
  	}
    
  	public static function queryLoad($db, $id_user) {
        $query = $db->prepare("SELECT *
                    			FROM user
                				WHERE id = :id_user
                				AND deleted != :deleted");
        $query->bindParam(':id_user', $id_user);
        $query->bindValue(':deleted', CT_DELETED);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
        	throw new QueryLoadUserException("Failed loading user with id " . $id_user . $errInfo);
        }
            
        $data = $query->fetch(PDO::FETCH_ASSOC);
        
        if (!isset($data['id_client'])) {
        	$query->closeCursor();
        	throw new QueryLoadUserException("Failed loading user with id = " . $id_user . " reason: id not existing");                    	
        }
        
        $user = new User($data['id_client'], $data['username'], $data['password']
                        	, $data['tip'], $data['email'], $data['change_pass']);
                            
        $user->id_user = (int)$id_user;
        $user->nume = $data['nume'];
        $user->prenume = $data['prenume'];

        $user->language = (int)$data['language'];
        $user->version = (int)$data['version'];
        $user->change_pass = (int)$data['change_pass'];
        $user->tooltip = (int)$data['tooltip'];
        $user->firme_per_pag = (int)$data['firme_per_pag'];
        $user->comenzi_per_pag = (int)$data['comenzi_per_pag'];
        $user->coloane_comenzi = $data['coloane_comenzi'];
        $user->coloane_firme = $data['coloane_firme'];
        $user->extra_text = $data['extra_text'];
        
        $user->moneda = $data['moneda'];
        
        $user->comenzi_plata_per_pag = $data['comenzi_plata_per_pag'];
        $user->comenzi_incasat_per_pag = $data['comenzi_incasat_per_pag'];
        
        $user->comanda_interna_externa = $data['comanda_interna_externa'];
        
        $user->comenzi_incarcari_per_pag = $data['comenzi_incarcari_per_pag'];
        $user->comenzi_descarcari_per_pag = $data['comenzi_descarcari_per_pag'];
        $query->closeCursor();
        return $user;
    }
    
    public static function queryLoadUserAtLogin($db, $username, $password) {
        $username = strtolower($username);
                
        $query = $db->prepare("SELECT *
                    			FROM user
                    			WHERE username = :username
                    			AND password = PASSWORD(:password)
                    			AND deleted != :deleted");
        
        $query->bindParam(':username', $username);
        $query->bindParam(':password', $password);
        $query->bindValue(':deleted', CT_DELETED);
                        
        if ($query->execute() == FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
        	throw new QueryLoadUserException("Bad user name or password" . $errInfo);
        }
            
        $data = $query->fetch(PDO::FETCH_ASSOC);
        
        if (!isset($data['id'])) {
        	$query->closeCursor();
        	throw new QueryLoadUserException("Failed loading user.!! Bad id?");                    	
        }
        
        $id_user = $data['id'];
        $query->closeCursor();
        $user = User::queryLoadUser($db, $id_user);
        return $user;
    }
    
    public function queryDelete($db) {
      	$query = $db->prepare("UPDATE user SET deleted = :deleted
      							WHERE id = :id_user");
      	
      	
      	$query->bindValue(':deleted', CT_DELETED);
      	$query->bindParam(':id_user', ($this->id_user));
      	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
      		throw new QueryDeleteUserException("Failed deleting user with id: " . $this->id_user . $errInfo);
      	}
    	  	
      	$query->closeCursor();      	
    }
    
    public static function queryLoadByClient($db, $id_client, $loadAdmin = true, $id_admin_user = 0) {
    	
        $query = $db->prepare("SELECT *
                    			FROM user
                    			WHERE id_client = :id_client
                    			AND deleted != :deleted");        
        $query->bindParam(':id_client', $id_client);
        $query->bindValue(':deleted', CT_DELETED);
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
        	throw new QueryLoadUserException("Failed loading users by client; client_id = " . $id_client . $errInfo);                    
        }
        
        $users = array();
        
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {         	
            $user = new User($data['id_client'], $data['username'], $data['password']
                            , $data['tip'], $data['email']);
            
            $user->id_user = (int)$data['id'];
            if ($loadAdmin == false && $user->id_user == $id_admin_user)
            	continue;
            	            	
            $user->nume = $data['nume'];
            $user->prenume = $data['prenume'];
            
            $user->version = (int)$data['version'];            
            $user->deleted = (int)$data['deleted'];
            $user->tooltip = (int)$data['tooltip'];
            $user->comenzi_per_pag = $data['comenzi_per_pag'];
            $user->firme_per_pag = $data['firme_per_pag'];     
            $user->coloane_comenzi = $data['coloane_comenzi'];
            $user->coloane_firme = $data['coloane_firme'];   

            $user->moneda = $data['moneda'];
        
        	$user->comenzi_plata_per_pag = $data['comenzi_plata_per_pag'];
        	$user->comenzi_incasat_per_pag = $data['comenzi_incasat_per_pag'];

        	$user->comanda_interna_externa = $data['comanda_interna_externa'];
        	
        	$user->comenzi_incarcari_per_pag = $data['comenzi_incarcari_per_pag'];
        	$user->comenzi_descarcari_per_pag = $data['comenzi_descarcari_per_pag'];
            $user->extra_text = $data['extra_text'];
            $users[] = $user;
        }     
        $query->closeCursor();
        return $users;
    }
    
    function gen_md5_salt($saltgen) {
		$salt = '$1$';
		while (strlen($salt) < 12) $salt .= $saltgen;
		return $salt;
	}
	
	//crypts a password using username for salt generation
	static function crypt_password($username, $password) {
		
		if (!isset($username) || !isset($password)) {
			return null;
		}
    
		// Get the crypt salt from the username
		$salt = gen_md5_salt($username);
		// Encrypt the password
    
		return crypt($password, $salt);	  
	}
	
	public static function getUserByUserAndPass($db, $username, $password) {
		
		$query = "SELECT * FROM user 
		        	WHERE username = '$username' 
		          	AND password = '$password'
		          	AND deleted != " . CT_DELETED . "
					LIMIT 1;";

		// Execute the query
		$result = $db->query($query);								

		if ($result === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
    	  	throw new QueryLoadUserException("Failed loading user at login") . $errInfo;
		}													
		
		$row = $result->fetch(PDO::FETCH_ASSOC);

		if ($row === FALSE) {
			throw new QueryLoadUserException("No user found");
		}
		
		$user = new User($row['id_client'], $row['username'], $row['password'], $row['tip'], $row['email']);
		$user->id_user = $row['id'];
		$user->id_client = $row['id_client'];
		$user->version = $row['version'];
		$user->nume = $row['nume'];
		$user->prenume = $row['prenume'];
        $user->tooltip = $row['tooltip'];
        $user->language = $row['language'];
        $user->comenzi_per_pag = $row['comenzi_per_pag'];
		$user->firme_per_pag = $row['firme_per_pag'];
        $user->coloane_comenzi = $row['coloane_comenzi'];
        $user->coloane_firme = $row['coloane_firme'];
        $user->change_pass = $row['change_pass'];
        $user->extra_text = $row['extra_text'];

        $user->moneda = $row['moneda'];
        
        $user->comenzi_plata_per_pag = $row['comenzi_plata_per_pag'];
        $user->comenzi_incasat_per_pag = $row['comenzi_incasat_per_pag'];

        $user->comanda_interna_externa = $row['comanda_interna_externa'];

        $user->comenzi_incarcari_per_pag = $row['comenzi_incarcari_per_pag'];
        $user->comenzi_descarcari_per_pag = $row['comenzi_descarcari_per_pag'];        
        
		return $user;		
	}
	
	public static function getUserByEmail($db, $email) {
		
		$query = "SELECT * FROM user 
		        	WHERE email = '$email' 
		          	LIMIT 1;";
				
		// Execute the query
		$result = $db->query($query);								

		if ($result === FALSE) {
    	  	throw new QueryLoadUserException("Failed loading user for email: ".$email);
		}													
		
		$row = $result->fetch(PDO::FETCH_ASSOC);
		if ($row == false) {
            $errInfo = $result->errorInfo();
            $errInfo = $errInfo[2];
			throw new QueryLoadUserException("No user found with email: $email" . $errInfo);
		}
		$user = new User($row['id_client'], $row['username'], $row['password'], $row['tip'], $row['email']);
		$user->id_user = $row['id'];
		$user->version = $row['version'];
		$user->tooltip = $row['tooltip'];
		$user->language = $row['language'];
        $user->comenzi_per_pag = $row['comenzi_per_pag'];
        $user->firme_per_pag = $row['firme_per_pag'];  
        $user->coloane_comenzi = $row['coloane_comenzi'];
        $user->coloane_firme = $row['coloane_firme']; 
        
        $user->moneda = $row['moneda'];
        
        $user->comenzi_plata_per_pag = $row['comenzi_plata_per_pag'];
        $user->comenzi_incasat_per_pag = $row['comenzi_incasat_per_pag'];
                
        $user->comanda_interna_externa = $row['comanda_interna_externa'];
        
        $user->comenzi_incarcari_per_pag = $row['comenzi_incarcari_per_pag'];
        $user->comenzi_descarcari_per_pag = $row['comenzi_descarcari_per_pag'];
        $user->extra_text = $row['extra_text'];        
		return $user;		
	}
	
	public function checkPassword($password) {
		return ($this->password == crypt_password($this->username, $password));
	}
	
	public function queryChangePassword($db) {
		$this->username = strtolower($this->username);
	    $this->version++;
	    
	    $query = $db->prepare("UPDATE user
	                			SET password = :password,
                                    version = :version,
                                    change_pass = :changePass
			                	WHERE id = :id_user
	                			AND version < :version");
	    
	    $this->password = crypt_password($this->username,   $this->password);
	    $query->bindParam(':password', 	$this->password,    PDO::PARAM_STR);
	    $query->bindParam(':version', 	$this->version,     PDO::PARAM_INT);
        $query->bindParam(':id_user',     $this->id_user,     PDO::PARAM_INT);
	    $query->bindValue(':changePass', 	0); //no more password changing needed
        
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
			throw new QueryUpdateUserException("Failed updating light user's " . $this->username . " password" . $errInfo);   
		}
		if ($query->rowCount() == 0) { 
			$query->closeCursor();
			throw new QueryWrongVersionException("Query update light wrong version on user: " . $this->username);
		}
	    	    
		$query->closeCursor();
        return CT_RESULT_OK;
	}
        
    public static function UpdateCampUser($db, $user_id, $camp, $value) {
            
        $query = "UPDATE user SET $camp = '" . ($value) . "' WHERE id = " . ($user_id);

        if (($result = $db->query($query)) === FALSE) {            
            $errInfo = $db->errorInfo();
            $errInfo = $errInfo[2];
            throw new QueryUpdateUserException("Failed updating profile for user with id: " . $user_id . $errInfo);
        }  
    }    
}

?>
